package redecolaborativa.client;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;

public class PainelPaginaInicial extends Composite {
	FachadaAsync fachada;
	String login, idSessao;
	AbsolutePanel absolutePanel, absolutePanel_1, painelAmigos;
	static ScrollPanel painelPrincipal;
	ScrollPanel painelGeral;
	HistoricoAtividades historicoAtividades;
	
	public PainelPaginaInicial(FachadaAsync fachada, ScrollPanel painelGeral, String login, String idSessao) {
		this.fachada = fachada;
		this.login = login;
		this.idSessao = idSessao;
		this.painelGeral = painelGeral;
		absolutePanel = new AbsolutePanel();
		absolutePanel_1 = new AbsolutePanel();
		
		initWidget(absolutePanel);
		absolutePanel.setSize("1267px", "484px");
		
		painelPrincipal = new ScrollPanel();
		absolutePanel.add(painelPrincipal, 260, 21);
		painelPrincipal.setSize("709px", "453px");
		
		historicoAtividades = new HistoricoAtividades(fachada, idSessao);
		painelPrincipal.add(historicoAtividades);
		historicoAtividades.setWidth("707px");
		
		CaptionPanel cptnpnlOpes = new CaptionPanel("OP\u00C7\u00D5ES");
		cptnpnlOpes.setStyleName("new-caption2");
		absolutePanel.add(cptnpnlOpes, 10, 10);
		cptnpnlOpes.setSize("194px", "448px");
		
		cptnpnlOpes.setContentWidget(absolutePanel_1);
		absolutePanel_1.setSize("196px", "426px");
		
		CaptionPanel cptnpnlNewPanel = new CaptionPanel("New panel");
		cptnpnlNewPanel.setStyleName("new-caption");
		cptnpnlNewPanel.setCaptionHTML("AMIGOS");
		absolutePanel.add(cptnpnlNewPanel, 999, 21);
		cptnpnlNewPanel.setSize("210px", "437px");
		
		ScrollPanel scrollPanelAmigos = new ScrollPanel();
		cptnpnlNewPanel.setContentWidget(scrollPanelAmigos);
		scrollPanelAmigos.setSize("207px", "420px");
		
		painelAmigos = new AbsolutePanel();
		scrollPanelAmigos.add(painelAmigos);
		painelAmigos.setSize("100%", "100%");
		
		cadastrarItem();
		pesquisarItem();
		mostraAtualizacoes();
		mostraNotificacoes();
		mostraItensEmprestados();
		mostraItensAdquiridos();
		mostraAmigos();
	}
	
	public static ScrollPanel getPainelGeral() {
		return painelPrincipal;
	}

	private void cadastrarItem() {
		Button btnCadastrarItem = new Button("Cadastrar Item");
		absolutePanel_1.add(btnCadastrarItem, 0, 31);
		btnCadastrarItem.setSize("196px", "45px");
		btnCadastrarItem.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				painelPrincipal.clear();
				painelPrincipal.add(new CadastroItem(fachada, login));
			}
		});
	}
	
	private void pesquisarItem() {
		Button btnPesquisarItem = new Button("Pesquisar Item");
		absolutePanel_1.add(btnPesquisarItem, 0, 99);
		btnPesquisarItem.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				painelPrincipal.clear();
				painelPrincipal.add(new PesquisaItens(painelGeral, painelPrincipal, login, fachada, idSessao));
			}
		});
		btnPesquisarItem.setSize("196px", "45px");
	}
	
	private void mostraAtualizacoes() {
		Button btnAtualizaes = new Button("Atualiza\u00E7\u00F5es");
		btnAtualizaes.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				painelPrincipal.clear();
				painelPrincipal.add(historicoAtividades);
			}
		});
		absolutePanel_1.add(btnAtualizaes, 0, 164);
		btnAtualizaes.setSize("196px", "45px");
	}
	
	private void mostraNotificacoes() {
		Button btnNotificaes = new Button("Notifica\u00E7\u00F5es");
		btnNotificaes.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				painelPrincipal.clear();
				painelPrincipal.add(new Notificacoes(fachada, painelGeral, idSessao, login));
			}
		});
		absolutePanel_1.add(btnNotificaes, 0, 230);
		btnNotificaes.setSize("196px", "45px");
	}
	
	private void mostraItensEmprestados() {
		Button btnItensEmprestados = new Button("Itens emprestados");
		btnItensEmprestados.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				painelPrincipal.clear();
				painelPrincipal.add(new RequisitaItensEmprestados(fachada, idSessao));
			}
		});
		absolutePanel_1.add(btnItensEmprestados, 0, 298);
		btnItensEmprestados.setSize("196px", "45px");
	}
	
	private void mostraItensAdquiridos() {
		Button btnItensAdquiridos = new Button("Itens adquiridos");
		btnItensAdquiridos.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				painelPrincipal.clear();
				painelPrincipal.add(new DevolveItemAdquirido(fachada, idSessao));
			}
		});
		absolutePanel_1.add(btnItensAdquiridos, 0, 371);
		btnItensAdquiridos.setSize("196px", "45px");
	}
	
	private void mostraAmigos() {
		try {
			fachada.getAmigos(idSessao, login, new AsyncCallback<List<String>>() {
				@Override
				public void onSuccess(List<String> result) {
					for(final String str : result) {
						String nome = str.split("-")[0];
						final Label amigo = new Label(nome);
						painelAmigos.add(amigo);
						amigo.addClickHandler(new ClickHandler() {
							@Override
							public void onClick(ClickEvent event) {
								painelGeral.clear();
								painelGeral.add(new Perfil(fachada, painelGeral, idSessao, login, str.split("-")[1]));
							}
						});
						eventoDeMouseOver(amigo);
					}
				}
				@Override
				public void onFailure(Throwable caught) {
					Window.alert(caught.getMessage());
				}
			});
		} catch (Exception e) {
			Window.alert(e.getMessage());
		}
	}
	
	private void eventoDeMouseOver(final Label label) {
		label.addMouseOverHandler(new MouseOverHandler() {
			@Override
			public void onMouseOver(MouseOverEvent event) {
				label.setStyleName("underlined");
			}
		});
		label.addMouseOutHandler(new MouseOutHandler() {
			@Override
			public void onMouseOut(MouseOutEvent event) {
				label.setStyleName("gwt-Label");								
			}
		});
	}
}
